import { userData } from '@/services/login';
import { reloadAuthorized } from './Authorized';
import cookie from 'react-cookies';
// use localStorage to store the authority info, which might be sent from server in actual project.
export function getAuthority(str?: string): string | string[] {
  const authorityString = typeof str === 'undefined' && localStorage ? localStorage.getItem('antd-pro-authority') : str;
  // authorityString could be admin, "admin", ["admin"]
  let authority;
  try {
    if (authorityString) {
      authority = JSON.parse(authorityString);
    }
  } catch (e) {
    authority = authorityString;
  }
  if (typeof authority === 'string') {
    return [authority];
  }
  // preview.pro.ant.design only do not use in your production.
  // preview.pro.ant.design 专用环境变量，请不要在你的项目中使用它。
  if (!authority && ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') {
    return ['admin'];
  }
  return authority;
}

export function setAuthority(authority: string | string[]): void {
  const proAuthority = typeof authority === 'string' ? [authority] : authority;
  localStorage.setItem('antd-pro-authority', JSON.stringify(proAuthority));
  // auto reload
  reloadAuthorized();
}

/**
 * 保存用户信息id
 * @param data
 */
export function setUserData(data: userData): void {
  sessionStorage.setItem('user-data', JSON.stringify(data));
}

/**
 * 获取用户信息id
 * @param data
 */
export function getUserData(): userData {
  const pop: any = sessionStorage.getItem('user-data');
  return JSON.parse(pop);
}

/**
 * 获取当前权限是否是管理员权限
 * @param type
 */
export function getAuthorityAdmin(type: string): string {
  let authority: string = '';

  switch (type) {
    case '1':
      authority = 'admin';
      break;

    case '2':
      authority = 'user';
      break;
    case '3':
      authority = 'tchs';
      break;
    case '4':
      authority = 'tch';
      break;

    case '5':
      authority = 'yAdmin';
      break;
  }
  return authority;
}

/**
 * 获取当前用户权限
 * @param type
 */
export function getAuthorityUser(type: string): string {
  let authority: string = '';

  switch (type) {
    case '1':
      authority = '管理员';
      break;

    case '2':
      authority = '作者';
      break;

    case '3':
      authority = '评审老师';
      break;

    case '4':
      authority = '审改老师';
      break;

    case '5':
      authority = '实体印章管理员';
      break;
  }
  return authority;
}

/**
 * 获取Tooken
 */
export function getAuthorization(): string {
  return cookie.load('Authorization');
}

/**
 * 删除Tooken
 */
export function removeAuthorization(): void {
  cookie.remove('Authorization', { path: '/' });
}
